package com.neusoft.service.impl;

import com.neusoft.entity.Prescription;
import com.neusoft.mapper.PrescriptionMapper;
import com.neusoft.service.PrescriptionService;
import com.neusoft.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author akemihomurasama
 */
//todo 根据药品id更改处方状态升级为直接根据处方id修改处方状态
@Service
public class PrescriptionServiceImpl implements PrescriptionService {
    @Autowired
    private PrescriptionMapper prescriptionMapper;

    /**
     * 向处方表中插入数据
     *
     * @param prescriptionList 要插入的数据
     * @return 插入结果
     */
    @Override
    public Result addPrescriptionList(List<Prescription> prescriptionList) {
        prescriptionList.forEach(prescription -> {
            prescription.setCreationTime(new Date());
            prescription.setDrugState("已开立");
        });
        prescriptionMapper.addPrescriptionList(prescriptionList);
        return Result.ok();
    }

    /**
     * 根据条件查询处方列表
     *
     * @param prescription 查询条件
     * @return 处方列表
     */
    @Override
    public Result queryPrescriptionByCondition(Prescription prescription) {
        return Result.ok(prescriptionMapper.queryPrescriptionByCondition(prescription));
    }

    /**
     * 患者结算，批量修改处方状态为已缴费
     *
     * @param drugIdList 药品id列表
     * @param registerId 患者id
     * @return 返回修改结果
     */
    @Override
    public Result patientDrugPay(List<Integer> drugIdList, int registerId) {
        prescriptionMapper.updateStateByRegisterId(drugIdList, "已缴费", registerId);
        return Result.ok();
    }

    /**
     * 给患者发药，修改处方状态为已发药
     *
     * @param drugIdList 药品id列表
     * @param registerId 患者id
     * @return 返回修改结果
     */
    @Override
    public Result giveDrug(List<Integer> drugIdList, int registerId) {
        prescriptionMapper.updateStateByRegisterId(drugIdList, "已发药", registerId);
        return Result.ok();
    }

    /**
     * 给患者退药，修改处方状态为已退药
     *
     * @param drugIdList 药品id列表
     * @param registerId 患者id
     * @return 返回修改结果
     */
    @Override
    public Result giveUpDrug(List<Integer> drugIdList, int registerId) {
        prescriptionMapper.updateStateByRegisterId(drugIdList, "已退药", registerId);
        return Result.ok();
    }

    /**
     * 处方列表退钱，修改床房状态为已退款
     *
     * @param prescriptionIdList 处方id列表
     * @return 修改结果
     */
    @Override
    public Result drugMoneyGive(List<Integer> prescriptionIdList) {
        prescriptionMapper.updateStateByPreIdList(prescriptionIdList, "已退费");
        return Result.ok();
    }
}
